package ${packageName}.controller;


import ${packageName}.entity.${classInfo.className};
import ${packageName}.dto.${classInfo.className}DTO;
import ${packageName}.service.${classInfo.className}Service;
import com.ibaboss.next.http.ResultEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Collection;
import java.lang.Boolean;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
 * Controller to ${classInfo.className}.
 *
 * @author ${authorName} ${.now?string('yyyy-MM-dd')}
 */
@RequiredArgsConstructor
@Api(value = "${classInfo.className}Controller",tags = "#${classInfo.classComment}# 管理")
@RestController
@RequestMapping("/api/${moduleName}/${classInfo.className?uncap_first}")
public class ${classInfo.className}Controller  {

    private final ${classInfo.className}Service ${classInfo.className?uncap_first}Service;

    private final ${classInfo.className}Cache ${classInfo.className?uncap_first}Cache;


    @ApiOperation(value = "根据ID查询", notes = "根据ID查询")
    @GetMapping("/findById")
    public ResultEntity<${classInfo.className}DTO> getById(@RequestParam String id) {
        ${classInfo.className} ${classInfo.className?uncap_first} = ${classInfo.className?uncap_first}Cache.findById(id);
        ${classInfo.className}DTO ${classInfo.className?uncap_first}DTO = Optional.ofNullable(${classInfo.className?uncap_first}).map(${classInfo.className}DTO::from).orElse(null);
        return ResultEntity.ok(${classInfo.className?uncap_first}DTO);
    }


    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping("/create")
    public ResultEntity<${classInfo.className}DTO> create(@Valid @RequestBody ${classInfo.className}DTO ${classInfo.className?uncap_first}DTO ) {
        ${classInfo.className} ${classInfo.className?uncap_first} = ${classInfo.className?uncap_first}DTO.to${classInfo.className}();
        String uuid = NextIdGenerator.uuid();
        ${classInfo.className?uncap_first}.setId(uuid);
        ${classInfo.className} ${classInfo.className?uncap_first}Result = ${classInfo.className?uncap_first}Cache.add(uuid,${classInfo.className?uncap_first});
        ${classInfo.className}DTO ${classInfo.className?uncap_first}resultDTO = Optional.ofNullable(${classInfo.className?uncap_first}Result).map(${classInfo.className}DTO::from).orElse(null);
       return ResultEntity.ok(${classInfo.className?uncap_first}resultDTO);
    }

    @ApiOperation(value = "修改", notes = "修改")
    @PutMapping("/update")
    public ResultEntity<${classInfo.className}DTO> update(@Valid @RequestBody ${classInfo.className}DTO ${classInfo.className?uncap_first}DTO ) {
        ${classInfo.className} ${classInfo.className?uncap_first} = ${classInfo.className?uncap_first}DTO.to${classInfo.className}();
        String uuid = ${classInfo.className?uncap_first}.getId();
        ${classInfo.className} ${classInfo.className?uncap_first}Result = ${classInfo.className?uncap_first}Cache.modify(uuid,${classInfo.className?uncap_first});
        ${classInfo.className}DTO ${classInfo.className?uncap_first}resultDTO = Optional.ofNullable(${classInfo.className?uncap_first}Result).map(${classInfo.className}DTO::from).orElse(null);
        return ResultEntity.ok(${classInfo.className?uncap_first}resultDTO);
    }

    @ApiOperation(value = "根据ID删除", notes = "根据ID删除")
    @DeleteMapping("/deleteById")
    public ResultEntity<String> deleteById(@RequestParam String id) {
       ${classInfo.className?uncap_first}Cache.deleteById(id);
       return ResultEntity.success();
    }



   @ApiOperation("分页请求, 请求格式GET /path/all?page=1&size=100&sort=id,weight,asc&sort=name,desc")
   @GetMapping("/list")
   public ResultEntity<Page<${classInfo.className}DTO>> list(${classInfo.className}DTO ${classInfo.className?uncap_first}DTO,@PageableDefault Pageable pageable) {
        ExampleMatcher matcher = ExampleMatcher.matchingAll();
        ${classInfo.className} ${classInfo.className?uncap_first} = ${classInfo.className?uncap_first}DTO.to${classInfo.className}();
        Example<${classInfo.className}> example = Example.of(${classInfo.className?uncap_first}, matcher);
        Page<${classInfo.className}DTO> dtoPage = ${classInfo.className?uncap_first}Cache.page(example, pageable).map(${classInfo.className}DTO::from);
        return ResultEntity.ok(dtoPage);
    }

}
